package com.song.osclient.view;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nineoldandroids.view.ViewPropertyAnimator;
import com.song.osclient.App;
import com.song.osclient.R;
import com.song.osclient.bean.Page;

import java.util.ArrayList;
import java.util.List;


public class CustomViewPageStrip extends LinearLayout {
    private Context context;
    private ColorStateList textColor;
    private float textSize;
    private int paddingLeft;
    private int indicatorHeight;
    private int indicatorColor;
    private LinearLayout linearLayout;
    private ImageView indicator;
    private int titleCount;
    private int itemWidth;

    public CustomViewPageStrip(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;

        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomViewPageStrip);
            if (typedArray != null) {
                textColor = typedArray.getColorStateList(R.styleable.CustomViewPageStrip_stripTextColor);
                textSize = typedArray.getFloat(R.styleable.CustomViewPageStrip_stripTextSize, 15);
                indicatorColor = typedArray.getColor(R.styleable.CustomViewPageStrip_stripIndicatorColor, Color.BLACK);
                indicatorHeight = typedArray.getDimensionPixelSize(R.styleable.CustomViewPageStrip_stripIndicatorHeight, App.dp2Px(1));
                paddingLeft = typedArray.getDimensionPixelSize(R.styleable.CustomViewPageStrip_stripPadding, App.dp2Px(1));
                typedArray.recycle();
            }
        }
    }

    public CustomViewPageStrip(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    public void bindViewPagerWithTitleData(ViewPager pager, List<Page> pageList) {
        titleCount = pageList.size();
        List<String> titleList = new ArrayList<>(titleCount);
        for (Page page : pageList) {
            titleList.add(page.getTitle());
        }
        initView(pager, titleList);
    }

    private void initView(final ViewPager pager, List<String> titleList) {
        setOrientation(VERTICAL);
        linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(HORIZONTAL);
        for (int i = 0; i < titleCount; i++) {
            TextView textView = new TextView(context);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER;
            layoutParams.weight = 1;
            textView.setTextColor(textColor);
            textView.setTextSize(textSize);
            textView.setPadding(paddingLeft, 15, paddingLeft, 15);
            textView.setTag(i);
            textView.setClickable(true);
            textView.setText(titleList.get(i));
            textView.setGravity(Gravity.CENTER_HORIZONTAL);
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    pager.setCurrentItem((Integer) v.getTag());
                }
            });
            linearLayout.addView(textView, layoutParams);
        }
        addView(linearLayout);
        linearLayout.getChildAt(0).setSelected(true);
        indicator = new ImageView(context);
        LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(getItemWidth(), indicatorHeight);
        indicator.setBackgroundColor(indicatorColor);
        addView(indicator, layoutParams1);
        View line = new View(context);
        line.setBackgroundColor(getResources().getColor(R.color.gray));
        addView(line);
        line.getLayoutParams().height = 1;
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                updateIndicator(position, positionOffset);
            }

            @Override
            public void onPageSelected(int position) {
                for (int i = 0; i < titleCount; i++) {
                    linearLayout.getChildAt(i).setSelected(i == position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        itemWidth = getItemWidth();
    }

    public void updateIndicator(int index, float offset) {
        ViewPropertyAnimator.animate(indicator).translationX(App.dp2Px(paddingLeft) + itemWidth * (index + offset)).setDuration(0);
    }


    private int getItemWidth() {
        return (App.getScreenWidth() - App.dp2Px(paddingLeft * 2)) / titleCount;
    }
}
